<template>
  <div class="apply">
    <div class="d-flex justify-content-between">
      <div>
        <div class="leftBar d-flex">
          <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="院系">
              <el-select v-model="searchForm.collegeName" placeholder="请选择院系">
                <el-option
                    v-for="item in collegeData"
                    :key="item.key"
                    :label="item.value"
                    :value="item.key">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="GetWork">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="rightBar">
        <el-popover
            placement="top"
            width="160"
            v-model="visible">
          <p>确定全部删除吗？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visible = false">取消</el-button>
            <el-button type="primary" size="mini" @click="handleClickDel(undefined)">确定</el-button>
          </div>
          <el-button slot="reference" type="danger" icon="el-icon-delete" data-bs-toggle="tooltip"
                     data-bs-placement="top"
                     title="全部删除" circle></el-button>
        </el-popover>

      </div>
    </div>
    <div class="applyBody">
      <el-table
          ref="singleTable"
          :data="workData"
          highlight-current-row
          :header-cell-style="{'text-align':'center'}"
          :cell-style="cellStyle"
          border
          style="width: 100%;background-color: white">
        <el-table-column
            type="index"
            label="序号"
            width="50">
        </el-table-column>
        <el-table-column
            label="作品"
            width="250"
            :show-overflow-tooltip='true'>
          <template slot-scope="scope">
            <span style="cursor: pointer" @click="downloadFile(scope.row)">
               <i class="iconfont icon-word"></i><br/>
               <span>{{ scope.row.path.slice(22) }}</span>
            </span>
          </template>
        </el-table-column>
        <el-table-column
            label="状态"
            width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status===0?'danger':'success'">{{
                scope.row.status === 0 ? '未评审' : "已评审"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="成绩"
            width="100">
          <template slot-scope="scope">
            {{scope.row.score.length!==0?scope.row.score[0].score:'未评审'}}
          </template>
        </el-table-column>
        <el-table-column
            label="学院"
            width="250">
          <template slot-scope="scope">
            {{ scope.row.user[0].collegeName|college }}
          </template>
        </el-table-column>
        <el-table-column
            label="创建时间"
            width="250">
          <template slot-scope="scope">
            {{ scope.row.createTime|time }}
          </template>
        </el-table-column>

        <el-table-column
            label="更新时间"
            width="250">
          <template slot-scope="scope">
            {{ scope.row.updateTime|time }}
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClickDel(scope.row._id)" type="danger" size="small">删除</el-button>

          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          class="mt-3"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import init from "../../../api/init";
import admin from "../../../api/admin";
import college from "../../../filter/college";
import time from "../../../filter/time";

export default {
  name: "work",
  data() {
    return {
      limit: 30,
      skip: 0,
      page: 1,
      size: 30,
      total: 1,
      visible: false,
      workData: [],
      searchForm: {
        collegeName: this.$store.getters["userStore/userInfo"].collegeName,
      },
      collegeData: init.college,
    }
  },
  mounted() {
    this.GetWork()
  },
  filters: {
    college,
    time
  },
  methods: {
    handleSizeChange(size) {
      this.size = size
      this.limit = size
      this.GetWork()
    },
    handleCurrentChange(size) {
      this.page = size
      this.GetWork()
    },
    handleClickDel(id){
      this.visible = false
      const param = {
        id
      }
      const loading = this.$loading({
        lock: true,
        text: '删除中...请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      admin.delWork(param).then(r => {
        if (r.data.status === '200') {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.GetWork()
          loading.close()
        }
      }).catch(() => {
        this.$message.error('删除失败');
        loading.close()
      })
    },

    //
    GetWork() {
      const param = {
        collegeName: this.searchForm.collegeName,
        limit: this.limit,
        skip: (this.page - 1) * this.size,
      }
      admin.GetWork(param).then(r => {
        this.workData = r.data.data.w
      })
    },
    // 下载文件
    downloadFile(obj) {
      const param = {
        path: obj.path,
        name: obj.name
      }
      const a = document.createElement("a");
      a.setAttribute("href", "http://127.0.0.1/Admin/DownloadFile?fileInfo=" + JSON.stringify(param));
      let clickEvent = document.createEvent("MouseEvents");
      clickEvent.initEvent("click", true, true);
      a.dispatchEvent(clickEvent);
    },

    cellStyle() {
      return {
        'text-align': 'center',
        'overflow': 'hidden',
        'text-overflow': 'ellipsis',
        'white-space': 'nowrap',
      }
    },
  }
}
</script>

<style scoped>
.icon-word {
  font-size: 32px;
  color: #54a0ff;
}
</style>